package com.woniu.cq.medical.common;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @BelongsProject: medical
 * @BelongsPackage: com.example.medical.common
 * @Author: Rofus
 * @CreateTime: 2020-09-05 11:03
 * @Description: 密码加密工具类
 */
public class EncryptUtil {
    public static  final  String ALGORITHM_NAME="SHA-256";
    public static  final  int HASH_ITERATIONS=1;

    /**
     * 加密方法
     * @param password
     * @return
     */
    public static String encrypt(String password){
        return encrypt(password,null);
    }

    /**
     * 加密方法
     * @param password
     * @return
     */
    public static String encrypt(String password,String salt){
        if(salt!=null){
            SimpleHash simpleHash = new SimpleHash(ALGORITHM_NAME,password, ByteSource.Util.bytes(salt),HASH_ITERATIONS);
            return simpleHash.toHex();
        }
        SimpleHash hash = new SimpleHash(ALGORITHM_NAME, password,null, HASH_ITERATIONS);
        return hash.toHex();
    }
}
